<template>
  <div class="app-container">
    <el-card>
      <div slot="header">
        <div class="tools-select">
          <el-form :inline="true" :model="form" ref="form">
            <el-form-item v-for="(item, index) in queryCondition" :key="index" :label="item.colComment" :rules="[
              {
                required: item.required,
                message: '该查询条件必填',
                trigger: 'blur',
              },
            ]">
              <el-input v-if="(item.colType === 'int' || item.colType === 'varchar') && !item.enumable"
                v-model="form[item.tableName + '_' + item.colName]" size="mini" clearable />

              <el-select v-model="form[item.tableName + '_' + item.colName]" placeholder="请选择" v-if="item.enumable"
                size="mini" clearable>
                <SelectOption :item="item"></SelectOption>
              </el-select>

              <el-date-picker v-if="item.colType === 'datetime'" v-model="form[item.tableName + '_' + item.colName]"
                size="mini" clearable type="datetime" placeholder="选择日期时间">
              </el-date-picker>

              <el-date-picker v-if="item.colType === 'date'" v-model="form[item.tableName + '_' + item.colName]"
                size="mini" clearable type="datetime" placeholder="选择日期时间">
              </el-date-picker>

            </el-form-item>
            <el-form-item>
              <el-button type="primary" size="mini" icon="el-icon-search" @click="handleQuery">查询
              </el-button>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" size="mini" icon="el-icon-download" @click="handleDownload">下载
              </el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
      <!-- 表格区 -->
      <el-table size="mini" stripe :data="tableData" border>
        <el-table-column align="center" label="序号" type="index" width="50px" />
        <el-table-column align="center" :label="item.showName" :prop="item.colComment" v-for="(item, index) in queryCols"
          :formatter="(row, column, index) => formatter(row, column, index, item)" :key="index">
        </el-table-column>
      </el-table>
      <el-pagination :current-page="this.form.pageNum" :page-sizes="[5, 10, 15, 20]" :page-size="this.form.pageSize"
        layout="total, sizes, prev, pager, next, jumper" :total="this.form.total" @size-change="handleSizeChange"
        @current-change="handleCurrentChange" />
    </el-card>
  </div>
</template>

<script>
import { getQueryCondition } from "@/api/sysQueryCondition";
import { getQueryCols } from "@/api/sysQueryItem";
import { queryPage, download } from "@/api/sysQueryMain";
import SelectOption from "@/components/SelectOption"
import { formatDayTime, formatDay } from '@/utils/time'

export default {
  components: { SelectOption },
  props: ["id"],
  data() {
    return {
      tableData: [],
      queryCols: [],
      queryCondition: [],
      form: {
        mainId: this.id,
        pageSize: 10,
        pageNum: 1,
        total: undefined,
      },
      rules: {
        requiredCheck: [
          {
            required: true,
            message: "该查询条件必填",
            trigger: "blur",
          },
        ],
      },
    };
  },
  mounted() {
    this.handleQueryCondition();
    this.handleQueryItem();
  },
  methods: {
    handleQuery() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          queryPage(this.form).then((res) => {
            if (res.code === 20000) {
              this.tableData = res.data.records;
              this.form.total = res.data.total;
            }
          });
        } else {
          return false;
        }
      });
    },
    handleQueryCondition() {
      getQueryCondition(this.id).then((res) => {
        if (res.code === 20000) {
          this.queryCondition = res.data;
        }
      });
    },
    handleQueryItem() {
      getQueryCols(this.id).then((res) => {
        if (res.code === 20000) {
          this.queryCols = res.data;
        }
      });
    },
    handleDownload() {
      download(this.form);
    },
    handleSizeChange(pageSize) {
      this.form.pageSize = pageSize;
      this.handleQuery();
    },
    handleCurrentChange(pageNum) {
      this.form.pageNum = pageNum;
      this.handleQuery();
    },
    formatter(row, column, index, item) {
      if (item && item.colType === 'datetime') {
        return formatDayTime(index);
      }
      if (item && item.colType === 'date') {
        return formatDay(index);
      }
      return index;
    }
  },
};
</script>
<style scoped>
.app-container {
  padding: 0 0;
  margin: 0 0;
}

.el-pagination {
  float: left;
  padding-top: 20px;
  padding-bottom: 20px;
}

.tools-select {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #ffffff;
  padding: 10px;
  border: 1px;
  border-radius: 5px;
}

.el-card__header {
  border-bottom: none;
}
</style>
